//
//  GQRecommandViewController.m
//  MyMusic
//
//  Created by sunny on 16/4/3.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQRecommandViewController.h"
#import "MoreButton.h"
#import "GQFindMusicReusableView.h"
#import "GQRecommandCell.h"
#import <AFHTTPSessionManager.h>
#import <MJExtension.h>
#import "GQPicturePlayerViewController.h"
#import "GQRecommandSongItem.h"
#import "GQRecommandMVItem.h"
#import "GQRecommandMVCell.h"
#import "GQRecommandTableViewItem.h"

//collectionView的identity
static NSString * const recommandMvCellId = @"recommandMvCellId";
static NSString * const recommandCellId = @"recommandCellId";
static NSString * const headerID = @"headerID";
#define cellH 160
#define cellW 110
#define itemLineSpace 10
#import "GQRecommandTableCell.h"
//tableView的identity
static NSString * const tableViewCell = @"tableViewCell";

@interface GQRecommandViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
/** tableView的尾部视图collectionView */
@property (nonatomic, weak)UICollectionView *collectionView;
/** 轮播图片的控制器（必须强引用，否则不能处理业务逻辑） */
@property (nonatomic, strong)GQPicturePlayerViewController *picturePlayerVc;
/** tableView的模型数组 */
@property (nonatomic, strong)NSArray<GQRecommandTableViewItem *> *tableItemArray;
/** collectionView 的模型数组 */
@property (nonatomic, strong)NSArray *itemArray;
/** collectionView mv的模型数组 */
@property (nonatomic, strong)NSArray *mvItemArray;
@end

@implementation GQRecommandViewController

#pragma mark - 懒加载
- (GQPicturePlayerViewController *)picturePlayerVc
{
    if (!_picturePlayerVc) {
        _picturePlayerVc = [GQPicturePlayerViewController collectionViewVController];
    }
    return _picturePlayerVc;
}

- (NSArray *)tableItemArray
{
    if (_tableItemArray == nil) {
        _tableItemArray = [NSArray array];
    }
    return _tableItemArray;
}

- (NSArray *)itemArray
{
    if (_itemArray == nil) {
        _itemArray = [NSArray array];
    }
    return _itemArray;
}

- (NSArray *)mvItemArray
{
    if (_mvItemArray == nil) {
        _mvItemArray = [NSArray array];
    }
    return _mvItemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.tableView.rowHeight = 100;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, GQGlobleTabBarHeight + itemLineSpace * 2 , 0);
    self.tableView.showsVerticalScrollIndicator = NO;
//    设置头部视图
    [self setupHeaderView];
    
//    设置尾部视图
    [self setupFooterView];
    
//    注册tableView的cell
    [self.tableView registerClass:[GQRecommandTableCell class] forCellReuseIdentifier:tableViewCell];
    
    //      注册collection的cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"GQRecommandCell" bundle:nil] forCellWithReuseIdentifier:recommandCellId];
    [self.collectionView registerNib:[UINib nibWithNibName:@"GQRecommandMVCell" bundle:nil] forCellWithReuseIdentifier:recommandMvCellId];
    
    //    注册头部视图
    [self.collectionView registerNib:[UINib nibWithNibName:@"GQFindMusicReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
    
//    请求数据
    [self loadData];
    
    
}

#pragma mark - 请求数据
- (void)loadData
{
    [SVProgressHUD showWithStatus:@"正在加载数据，请稍后。。。。。"];
    
    [GQHttpRequest GET:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.7.2.0&channel=xiaomi&operator=2&method=baidu.ting.plaza.index&cuid=E6B77D59F4F7B2824CC57E6E5CADD436" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.itemArray = [GQRecommandSongItem mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"diy"][@"result"]];
        self.mvItemArray = [GQRecommandMVItem mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"mix_5"][@"result"]];
        self.tableItemArray = [GQRecommandTableViewItem mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"entry"][@"result"]];
        
//        确定cell的行数
        NSInteger col = 3;
        NSInteger row = (self.itemArray.count - 1 )/ col + 1;
//        获取流水布局
        UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
//        计算collectionView的高度 (每组cell个高度 * 组数) + 内边距
        CGFloat collectionHeight = (row * cellH + layout.headerReferenceSize.height + itemLineSpace) * 2;
        self.collectionView.gq_height = collectionHeight;
        self.tableView.tableFooterView = self.collectionView;
        
//        刷新数据
        [self.collectionView reloadData];
        [self.tableView reloadData];
        
        [SVProgressHUD dismiss];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"test2");
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - 设置tableView的头部和尾部视图
- (void)setupHeaderView
{
    //    设置头部视图的高度
    self.picturePlayerVc.view.gq_height = 200;
    self.tableView.tableHeaderView = self.picturePlayerVc.view;

}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
//    设置头部视图的高度
    self.picturePlayerVc.view.gq_height = 200;
}

- (void)setupFooterView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(cellW, cellH);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = itemLineSpace;
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    //    设置组头部视图
    layout.headerReferenceSize = CGSizeMake(375, 40);
    CGFloat collectionViewH = 500;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,GQUIScreenW ,collectionViewH ) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView = collectionView;
    //    隐藏垂直指示器
    collectionView.showsVerticalScrollIndicator = NO;
    self.tableView.tableFooterView = collectionView;
    
    //    设置数据源和代理
    collectionView.dataSource = self;
    collectionView.delegate = self;

}



#pragma mark - tableView DataScource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GQRecommandTableCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCell forIndexPath:indexPath];
    cell.tableItemArray = self.tableItemArray;
    
    return cell;
}


#pragma mark - collectionView的数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.itemArray.count;
    }
    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {//推荐歌单
        GQRecommandCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:recommandCellId forIndexPath:indexPath];
        GQRecommandSongItem *recommandSongItem = self.itemArray[indexPath.row];
        cell.recommandSongItem = recommandSongItem;
        return cell;
    }
  
    //mv
    GQRecommandMVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:recommandMvCellId forIndexPath:indexPath];
    GQRecommandMVItem *recommandMvItem = self.mvItemArray[indexPath.row];
    cell.recommandMVItem = recommandMvItem;
    return cell;
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader){
        
        GQFindMusicReusableView *headerV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            headerV.imageView.image = [UIImage imageNamed:@"cm2_discover_icn_recmd"];
            headerV.title_label.text = @"歌单推荐";
        } else if (indexPath.section == 1) {
            headerV.imageView.image = [UIImage imageNamed:@"cm2_discover_icn_movie"];
            headerV.title_label.text = @"MV";
        }
        reusableview = headerV;
    }
    
    return reusableview;
}


@end
